package com.macro.mall.portal.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.domain.ConfirmOrderResult;
import com.macro.mall.domain.CreateProductParam;
import com.macro.mall.domain.OrderParam;
import com.macro.mall.portal.service.OmsCartItemService;
import com.macro.mall.portal.service.OmsPortalOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 订单管理Controller
 * Created by macro on 2018/8/30.
 */
@Log4j2
@Controller
@Api(tags = "OmsPortalOrderController", description = "订单管理")
@RequestMapping("/order")
public class OmsPortalOrderController {

    @Autowired
    private OmsPortalOrderService portalOrderService;
//
//    @ApiOperation("根据购物车信息生成确认单信息")
//    @RequestMapping(value = "/generateConfirmOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult<ConfirmOrderResult> generateConfirmOrder() {
//        ConfirmOrderResult confirmOrderResult = portalOrderService.generateConfirmOrder();
//        return CommonResult.success(confirmOrderResult);
//    }

    @ApiOperation("生成订单")
    @RequestMapping(value = "/generateOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object generateOrder(@RequestParam String reqJson) {
        OrderParam orderParam = JSONArray.parseObject(reqJson, OrderParam.class);
        return portalOrderService.generateOrder(orderParam);
    }

    @ApiOperation("查询订单信息")
    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    @ResponseBody
    public Object getOrderList(@RequestParam String reqJson,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        OrderParam orderParam = JSONArray.parseObject(reqJson, OrderParam.class);
        return CommonResult.success(portalOrderService.getOrderList(orderParam, pageNum, pageSize));
    }

    @ApiOperation("查询订单商品信息")
    @RequestMapping(value = "/getOrderItemList", method = RequestMethod.POST)
    @ResponseBody
    public Object getOrderItemList(@RequestParam String reqJson) {
        OrderParam orderParam = JSONArray.parseObject(reqJson, OrderParam.class);
        return CommonResult.success(portalOrderService.getOrderItemList(orderParam.getOrderId()));
    }

    @ApiOperation("支付成功的回调")
    @RequestMapping(value = "/paySuccess")
    @ResponseBody
    public Object paySuccess(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("支付成功的回调");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(request.getInputStream(), "UTF-8"));
        String line;
        String resu = "";
        while ((line = reader.readLine()) != null) {
            resu += line;
        }
        log.info("支付成功的回调,请求参数为：{}",resu);
        WxPayOrderNotifyResult wxPayOrderNotifyResult = BaseWxPayResult.fromXML(resu, WxPayOrderNotifyResult.class);
        log.info("wxPayOrderNotifyResult:{}",wxPayOrderNotifyResult);
        log.info("wxPayOrderNotifyResult:{}",wxPayOrderNotifyResult);
        log.info("wxPayOrderNotifyResult:{}",wxPayOrderNotifyResult);
        log.info("wxPayOrderNotifyResult:{}",wxPayOrderNotifyResult);
        log.info("【订单号：】:{}",wxPayOrderNotifyResult.getOutTradeNo());
        return portalOrderService.paySuccess(Long.parseLong(wxPayOrderNotifyResult.getOutTradeNo()));
    }

    @ApiOperation("修改订单状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object updateStatus(@RequestParam Long orderId, @RequestParam Integer status) {
        return CommonResult.success(portalOrderService.updateStatus(orderId, status));
    }

    @ApiOperation("自动取消超时订单")
    @RequestMapping(value = "/cancelTimeOutOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object cancelTimeOutOrder() {
        return CommonResult.success(portalOrderService.cancelTimeOutOrder());
    }

    @ApiOperation("取消单个超时订单")
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult cancelOrder(Long orderId) {
        portalOrderService.sendDelayMessageCancelOrder(orderId);
        return CommonResult.success(null);
    }
}
